package com.zhkjnet.zhyy.util;

import com.zhkjnet.zhyy.entity.OilCardType;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ZhApi {

    /**
     * 中石油
     */
    private final static String ZSY = "cnpc";

    /**
     * 中石化
     */
    private final static String ZSH = "sinopec";

    /**
     * 签名
     * @param appKey
     * @param appSecret
     * @param gasnumber  卡号
     * @param gasname  持卡人
     * @param gasphone  手机
     * @param money 金额
     * @Param gastype  类型
     * @return
     */
    public static String sign(String appKey, String appSecret, String gasnumber, String gasname, String gasphone, Long money, OilCardType type) {
        Map<String, String> data = new HashMap<>();
        data.put("appKey", appKey);
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("money", String.valueOf(money));
        data.put("gastype", type == OilCardType.ZSY ? ZSY : ZSH);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String sign = DigestUtils.md5DigestAsHex((appSecret + builder.toString()).getBytes());
        return builder.append("sign=").append(sign).toString();
    }
    
    public static String sign2(String appKey, String appSecret, String gasnumber, String gasname, String gasphone, Long money, String orderId) {
        Map<String, String> data = new HashMap<>();
        data.put("appKey", appKey);
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("money", String.valueOf(money));
        data.put("orderId", orderId);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String sign = DigestUtils.md5DigestAsHex((appSecret + builder.toString()).getBytes());
        return builder.append("sign=").append(sign).toString();
    }

    public static boolean check(String sign, String appKey, String appSecret, String gasnumber, String gasname, String gasphone, Long money, OilCardType type) {
        Map<String, String> data = new HashMap<>();
        data.put("appKey", appKey);
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("money", String.valueOf(money));
        data.put("gastype", type == OilCardType.ZSY ? ZSY : ZSH);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String result = DigestUtils.md5DigestAsHex((appSecret + builder.toString()).getBytes());
        return result.equals(sign);
    }

}
